package com.aptech.yuanhuabao.api.soap.parser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SoapGenerator {
	private static final String SIGNKEY = "02694EEEBA69401A86B3664CD2CD445A";
	private static final String TAG = "SoapGenerator";
	// /http://210.13.82.203:8080/interface/webservice/MobileClientService?wsdl

	public static final String SUCCESS_RESULT = "SUCCESS";
	public static final String PARAM_INVALID = "PARAM_INVALID";// 参数格式不正确
	public static final String PHONE_AUTH_FAILED = "PHONE_AUTH_FAILED";// 号码认证失败
	public static final String PHONE_NUMBER_INVALID = "PHONE_NUMBER_INVALID";// 号码格式不正确或用户不存在
	public static final String OUTBOUND_REQ_NOT_EXIST = "OUTBOUND_REQ_NOT_EXIST";// 指定的会议不存在
	public static final String ERROR = "ERROR";// 其它错误

	public static final String PARAM_OUTOFRANGE = "PARAM_OUTOFRANGE";// 与会者超过最大人数
	public static final String QRCODE_NOT_MATCH = "QRCODE_NOT_MATCH";// 特征码不匹配
	public static final String SIGNATURE_NOT_MATCH = "SIGNATURE_NOT_MATCH";// 签名错误
	public static final String DATEFORMAT_INVALID = "DATEFORMAT_INVALID";// 时间格式不正确
	public static final String OUTBOUND_DISABLED = "OUTBOUND_DISABLED";// 外呼功能未开通
	public static final String CONF_ACTIVED_NOT_FOUND = "CONF_ACTIVED_NOT_FOUND";// 未找到有效的会议室号码

	/*
    public static String getSoapResult(Map<String, String> params, String method) {
		String result = "";
		String paramStr = "";
		String strSoapLabel = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " + "xmlns:mob=\"http://mobile.webservice.ipc.net2com.com/\">"
				+ "<soapenv:Header/><soapenv:Body>" + "%s</soapenv:Body></soapenv:Envelope>";
		String strParams = "<mob:%s>%s</mob:%s>";
		String paramFormat = "<%s>%s</%s>";

		for (String key : params.keySet()) {
			String value = params.get(key);
			if (!TextUtils.isEmpty(value)) {
				if ("sign".equals(key)) {
					value = EncryptUtil.MD5(URLEncoder.encode(value + HTTPConfig.SIGNKEY)).toUpperCase();
				}
				else {
					value = URLEncoder.encode(value);
				}
			}
			paramStr = paramStr + String.format(paramFormat, key, value, key);
		}

		String dataStr = String.format(strParams, method, paramStr, method);
		result = String.format(strSoapLabel, dataStr);

		return result;
	}

	public static String getSoapResult(LinkedHashMultimap<String, String> params, String method) {

		String result = "";
		String Command;

		Command = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
				+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body>" + "%s</soap:Body></soap:Envelope>";
		String Params = "<%s xmlns=\"http://mobile.webservice.ipc.net2com.com/\">%s</%s>";
		String paramFormat = "<%s xmlns=\"\">%s</%s>";

		String paramStr = "";
		Iterator iter = params.entries().iterator();
		while (iter.hasNext()) {
			Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();
			paramStr = paramStr + String.format(paramFormat, entry.getKey(), entry.getValue(), entry.getKey());

		}

		String dataStr = String.format(Params, method, paramStr, method);
		result = String.format(Command, dataStr);

		return result;
	}*/

	public static String getReturnStr(String data) {
		String result = "";
		StringBuffer reStr = new StringBuffer();
		reStr.append("<");
		reStr.append("return");
		reStr.append(">");
		// reStr.append("(.*?)"); //无法匹配含换行的文本，因此采用下面的写法
		reStr.append("([\\s\\S]*)");
		reStr.append("</");
		reStr.append("return");
		reStr.append(">");
		try {
			Pattern pattern = Pattern.compile(reStr.toString());
			Matcher matcher = pattern.matcher(data);
			if (matcher.find()) {
				result = matcher.group(1);
				result = result.replace("<![CDATA[", "").replaceAll("]]>", "").trim();
			}
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		return result;
	}

	public static String getXmlTagValue(String data, String tag) {
		String result = "";
		StringBuffer reStr = new StringBuffer();
		reStr.append("<");
		reStr.append(tag);
		reStr.append(">");
		reStr.append("([\\s\\S]*)");
		reStr.append("</");
		reStr.append(tag);
		reStr.append(">");

		Pattern pattern = Pattern.compile(reStr.toString());
		Matcher matcher = pattern.matcher(data);
		if (matcher.find()) {
			result = matcher.group(1);
			result = result.replace("<![CDATA[", "").replaceAll("]]>", "").trim();
		}
		return result;
	}

	public static String htmlEscape(String input) {
		input = input.replaceAll("&", "&amp;");
		input = input.replaceAll("<", "&lt;");
		input = input.replaceAll(">", "&gt;");
		input = input.replaceAll(" ", "&nbsp;");
		input = input.replaceAll("'", "&#39;"); // IE暂不支持单引号的实体名称,而支持单引号的实体编号,故单引号转义成实体编号,其它字符转义成实体名称
		input = input.replaceAll("\"", "&quot;"); // 双引号也需要转义，所以加一个斜线对其进行转义
		input = input.replaceAll("\n", "<br/>"); // 不能把\n的过滤放在前面，因为还要对<和>过滤，这样就会导致<br/>失效了
		return input;
	}

	public static String htmlUnescape(String input) {
		input = input.replaceAll("&amp;", "&");
		input = input.replaceAll("&lt;", "<");
		input = input.replaceAll("&gt;", ">");
		input = input.replaceAll("&nbsp;", " ");
		input = input.replaceAll("&#39;", "'"); // IE暂不支持单引号的实体名称,而支持单引号的实体编号,故单引号转义成实体编号,其它字符转义成实体名称
		input = input.replaceAll("&quot;", "\""); // 双引号也需要转义，所以加一个斜线对其进行转义
		input = input.replaceAll("<br/>", "\n"); // 不能把\n的过滤放在前面，因为还要对<和>过滤，这样就会导致<br/>失效了
		return input;
	}
}
